class observer {
    constructor (data) {
        this.walk(data);
    }
    walk (data) {
        if (!data || typeof data !== 'object') return;
        // 遍历data对象所有属性
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key]);
        });
    }
    defineReactive (data, key, val) {
        const self = this;
        let dep = new Dep();
        // 递归遍历data
        self.walk(val);
        Object.defineProperty(data, key, {
            enumerable: true,
            configurable: true,
            get () {
                Dep.target && dep.addSub(Dep.target);
                return val;
            },
            set (newVal) {
                if (newVal === val) return;
                val = newVal;
                self.walk(newVal);
                // 发送通知
                dep.notify();
            }
        });
    }
}